#!/bin/sh
#
# Attempt to backup and restore a file with the delta plugin
#

TestName="delta-test"
JobName=pluginTest
. scripts/functions

scripts/cleanup
scripts/copy-plugin-confs

cat << EOF >> $conf/susan-dir.conf
FileSet {
 Name = "FS_DELTA"
 Include {
  Options {
    signature=MD5
  }
 Plugin = "delta-test"
 }
}
EOF

$bperl -e 'add_attribute("$conf/susan-dir.conf", "FileSet", "FS_DELTA", "Job", "pluginTest")'
$bperl -e 'add_attribute("$conf/susan-dir.conf", "Accurate", "yes", "Job", "pluginTest")'

# install the delta plugin
make -C build/src/plugins/fd install-test-plugin >/dev/null

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=TestVolume001
setdebug level=150 client=$CLIENT
estimate job=$JobName level=Full
setdebug level=50 client=$CLIENT
run job=$JobName comment="read 400 bytes of passwd" yes
wait
status client=$CLIENT
messages
run job=$JobName comment="read 100 bytes of group" level=Incremental yes
wait
messages
run job=$JobName comment="read 100 bytes of hosts" level=Incremental yes
wait
messages
run job=$JobName comment="read 100 bytes of services" level=Incremental yes
wait
messages
status client=$CLIENT
quit
END_OF_DATA

run_susan

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
messages
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
setdebug level=50 client=$CLIENT
restore where=$tmp select all done
yes
wait
messages
@exec "mv $tmp/delta.txt $tmp/delta.OK.txt"
restore where=$tmp jobid=1
m *
done
yes
wait
messages
@$out ${cwd}/tmp/log3.out
status client
setdebug level=0 client=$CLIENT
quit
END_OF_DATA

run_bconsole

check_two_logs

stop_susan

head -c 50 /etc/passwd > $tmp/source
head -c 50 /etc/group >> $tmp/source
head -c 50 /etc/hosts >> $tmp/source
head -c 100 /etc/services >> $tmp/source
head -c 400 /etc/passwd | tail -c 150 >> $tmp/source

diff -u $tmp/source $tmp/delta.OK.txt > $tmp/d
if [ $? -ne 0 ]; then
    print_debug "ERROR: delta.OK.txt and source are different, restore failed"
    dstat=1
fi

head -c 400 /etc/passwd > $tmp/source2
diff -u $tmp/source2 $tmp/delta.txt > $tmp/d
if [ $? -ne 0 ]; then
    print_debug "ERROR: delta.txt and source2 are different, restore failed"
    dstat=1
fi



end_test
